<?php

class FormWindow extends WBResizableWindow { 
	
	private $_projectManager = null;
	private $_capture = false;
	private $_control = array();
	private $_currentControl = null;
	
	public static $property = array(
		'wbtype',
		'name',
		'title',
		'x',
		'y',
		'width',
		'height',
		'visible'
	);		
	
	private $_treeNode = null;
	
	public function __construct($options = array()) { 
		parent::__construct($options);
	}
	
	public function __destruct() { 
		parent::__destruct();
	}
	
	public function initComponent() { 
		parent::initComponent();

		$this->setTitle("(Empty Project)");
		$this->setDefaultStyle();
		$this->setArea('minsize', 0, 0, 80, 60);
		$this->setIcon(RESOURCE_HOME.'form.ico');
		$this->setTreeNode($this['treeNode']);
		
		$this->_projectManager = ProjectManager::$mainWindow->getProjectManager();
		
		$this->resetForm();
	}
	
	public function setTreeNode($node) { 
		$this->_treeNode = $node;
	}
	
	public function getTreeNode() { 
		return $this->_treeNode;
	}	
	
	/**
	 * 
	 * @return ProjectManager
	 */
	public function getProjectManager() { 
		return $this->_projectManager;
	}
	
	public function setDefaultStyle() { 
		$this->extern('DeleteWindowStyle', (WBUser::WS_MAXIMIZEBOX | WBUser::WS_MINIMIZEBOX));
		$this->extern('EnableCloseButton', false);
	}
	
	public function resetForm() {
		$pos = ProjectManager::$mainWindow->getPosition();
		
		$this->setPosition($pos['x'] + 330, $pos['y'] + 75);
		$this->setVisible(true);
	}
	
	public function createControl($type, $options = array()) { 
		$obj = ProjectManager::getControlTemplate($type, $options);
		
		$this->add($obj);  

		return $obj;
	}
	
	public function add($control) { 
		$this->_control[] = $control;
	}
	
	public function remove($i) { 
		unset($this->_control[$i]);
		
		$this->_control = array_merge(array(), $this->_control);
	}
	
	public function removeById($id) {
		$temp = -1;
		
		foreach ($this->_control as $i => $control) { 
			if ($control['id'] == $id) {
				$temp = $i; 
				break;
			}
		}
		
		if ($temp > -1) { 
			$this->remove($temp);
		}
	}
	
	public function length() { 
		return sizeof($this->_control);
	}	
	
	public function get($i) { 
		return $this->_control[$i];
	}

	public function getControlList() { 
		return $this->_control;	
	}
	
	public function getComponentList() { 
		return $this->getControlList();
	}
	
	public function setCurrentControl($control) { 
		$this->_currentControl = $control;
	}
	
	/**
	 * 
	 * 
	 * @return FormControl
	 */
	public function getCurrentControl() { 
		return $this->_currentControl;
	}
	
	public function repaint(WBGraphics $g) {
		$this->paintBackground($g);
		$this->paintControl($g);		
		$this->paintGhost($g);
	}
	
	public function onDragStart($eo) { 

		if ($this->_currentControl) {
			$handle = $this->_currentControl['handle'];
			if ($handle == FormControl::NOWHERE || $handle == FormControl::INCONTROL)  { 
				$this->clickToPoint($eo);
			} 
		} else { 		
			$this->clickToPoint($eo);
		}
		
		$this->updateControl();
		$this->refresh(true);
	}
	
	public function onDrag($eo) {

		$control = $this->getCurrentControl();		

		if ($control) {
			if ($control['handle'] == FormControl::INCONTROL) {  	
				$control->setMovePosition($eo->getX(), $eo->getY());				
			} else if ($control['handle'] == FormControl::NOWHERE)  { 
				
			} else { 
				$control->setScale($eo);		
			}
			
			$this->refresh(true);
		}

	}
	
	public function onDragEnd($eo) {
	 
		$this->initCurrentControl();
		$this->setCursor(WBUser::IDC_ARROW);		
		
		$this->updateControl(true);
		$this->refresh(true);

	}
	
	public function updateControl() { 
		$this->_projectManager->updateControl();
	}
	
	public function initCurrentControl() {
		if ($this->_currentControl) {
			$this->_currentControl->initData();
		}
	}
	
	
	public function onMouseMove($eo) {

		if (!($eo->isLeftButton() || $eo->isRightButton())) { 
			if ($this->_currentControl) { 
				$this->_currentControl->setMouseCursor($eo);
			}
		}
	}
	
	public function setSelectedControl($control) { 
		if ($this->_currentControl) {
			$this->_currentControl['handle'] = FormControl::NOWHERE; 
			$this->_currentControl->selected = false;
		}
		
		$control->selected = true;
		$control['handle'] = FormControl::INCONTROL;				
		$this->setCurrentControl($control);		
		
	}
	
	public function setCursor($cursor) { 
		$this->extern('user.SetMyCursor', $cursor);
	}
	
	public function clickToPoint(WBEvent $eo) { 


		$keys = array_keys($this->_control);
		rsort($keys);
		
		$i = 0;
		
		foreach ($keys as $key) {
			$control = $this->_control[$key]; 
	
			// check control in area 
			$selectedControl = $control->checkPoint($eo->x, $eo->y);
			
			if ($selectedControl) {

				$selectedControl->setMousePosition($eo->x, $eo->y);
				if ($selectedControl['treeNode']) { 
					$selectedControl['treeNode']->select(true);
				}
				
				$i++;
				break;

			} else { 
				$control->selected = false;
			} 
		}
		
		if ($i == 0) { 
			$this->setCurrentControl(null);
		} 
	}

	public function moveToCurrentControl($x, $y) { 

		if ($this->_currentControl) {		
			$this->setCursor(WBUser::IDC_SIZENS);			
			$this->_currentControl->setMovePosition($x, $y);
		}
		
		$this->refresh(true);
	}
	
	public function scaleToCurrentControl($x, $y) { 
 
		if ($this->_currentControl) {		
			$this->setCursor(WBUser::IDC_SIZENS);			
			$control->setMovePosition($x, $y);
		}
		
		$this->refresh(true);
	}		
	
	/**
	 * 
	 * @param unknown_type $g
	 * @return WBGraphics
	 */
	public function paintBackground(WBGraphics $g) { 
		
		$isWire  = ProjectManager::$checkWireFrame->isChecked();
		$isGrid  = ProjectManager::$checkSnapInGrid->isChecked();
		
		$size = $this->getSize(true);
		
		$g->rect(0, 0, $size['width'], $size['height'], 0xE4E2E2, true);
		
		if ($isWire) {
			for ($y = 0; $y < $size['height']; $y+= 10) {
				$g->line(0, $y, $size['width'], $y, 0xC8D8D8);
			}
			
			for ($x = 0; $x < $size['width']; $x += 10) {
				$g->line($x, 0, $x, $size['height'], 0xC8D8D8);
			}			
		}
	}
	
	public function renderToControl(WBGraphics $g) { 
		
		$list = $this->getControlList();
		
		foreach ($list as $id => $obj) { 
			$obj->display($g);
		}
		
	}	
	
	public function paintControl($g) {
		
		$this->doLayout();
		
		$this->renderToControl($g);
	}
	
	public function doLayout() { 
		$layout = $this['layout'];		

		if ($layout) { 
			
			if (is_string($layout) || is_array($layout)) { 
				$this['layout'] = wblayout($layout);
			}
			
			$this['layout']->render($this);
		}
	}	
	
	public function paintGhost($g) {
		$control = $this->getCurrentControl();
		if ($control) { 
			if ($control->isSelected()) { 
				$control->displayForSelected($g);
			}
		}
	}	
	
	public function toData() { 
		$temp = array('dataType' => 'window');
		$size = $this->getSize();
				
		$data = array();
		$data['wbtype'] = wbclass($this['class']);
		$data['x'] = 0;
		$data['y'] = 0;
		$data['width'] = $size['width'];
		$data['height'] = $size['height'];
		$data['title'] = $this->getTitle();
		$data['visible'] = $this->isVisible();
		$data['name'] = $this['name'];
		$data['layout'] = $this->getLayout();
		
		foreach ($data as $itemType => $value) { 
			
			$obj = FormControl::$attribute[$itemType];
			
			if ($obj['type'] == 'int') { 
				$value = sprintf($obj['format'], $value);
			} else if ($obj['type'] == 'layout') { 
				if (is_array($value)) { 
					$value = $value['layout'];
				}
			} 
	
			$temp[$itemType] = array('type' => $itemType, 'text' => $obj['text'], 'editor' => $obj['editor'], 'value' => $value);
			
		}
		
		return $temp;

	}

	public function toPropertyList() {
		if ($this->_currentControl) { 
			return $this->_currentControl->toPropertyList();
		} else {
			return $this->toData(); 
		}
	}

	
	public function getXmlList() { 
		$data = array();

		$data['wbtype'] = wbclass($this['class']);
		$data['x'] = 0;
		$data['y'] = 0;
		$data['width'] = $size['width'];
		$data['height'] = $size['height'];
		$data['title'] = $this->title;
		$data['visible'] = $this->isVisible();
		$data['name'] = $this['name'];
			
		return $data;
	}	

	public function toXML() { 
		
		$root = x_('window');
		
		$options = x_('options');
		
		foreach ($this->getXmlList() as $key => $value) { 
			$options->{$key}($value);
		}	
		
		$root->add($options);
		
		$controls = x_('controls');
		
		foreach ($this->getComponentList() as $control) {
			$controls->add($control->toXML());
		}		

		$root->add($controls);
		
		return $root->toString();	
	}
	
	public function update($data) {
		$control = $this->getCurrentControl();
		
		// update status 
		if ($control) { 
			$control->update($data);	
		} else {
			foreach ($data as $key => $value) {
				switch($key) {  
					case 'title': $this->setTitle($value);break;
					case 'width':
						$size = $this->getSize();
						$size['width'] = intval($value);
						$this->setSize($size['width'], $size['height']);
						break; 
					case 'height': 
						$size = $this->getSize();
						$size['height'] = intval($value);
						$this->setSize($size['width'], $size['height']);
						break;
					case 'x':
					case 'y': 
						break;			
					default:
						$this[$key] = $value;
						break;
				}
			}			
		}
		
		// redraw form window 
		$this->refresh(true);
	}	
	
}

?>